<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理</title>
    <script src="lib/js/jquery-3.7.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group input, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .btn-group {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
        }

        button {
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-warning {
            background-color: #ffc107;
            color: #212529;
        }

        .btn-danger {
            background-color: #dc3545;
            color: white;
        }

        .btn-success {
            background-color: #28a745;
            color: white;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .action-buttons {
            display: flex;
            gap: 5px;
        }

        .message {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .hidden {
            display: none;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 5% auto;
            padding: 20px;
            border-radius: 5px;
            width: 500px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .modal-header h2 {
            margin: 0;
        }

        .close {
            color: #aaa;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        .close:hover {
            color: black;
        }

        /* 查询条件区域 */
        .search-panel {
            background-color: white;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .search-form {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
        }

        /* 分页 */
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 5px;
        }

        .pagination button {
            padding: 8px 12px;
        }

        .pagination .active {
            background-color: #007bff;
            color: white;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>学生管理</h1>

    <!-- 消息提示 -->
    <div id="message" class="message hidden"></div>

    <!-- 查询条件 -->
    <div class="search-panel">
        <h3>查询条件</h3>
        <form id="search-form">
            <div class="search-form">
                <div class="form-group">
                    <label for="search-id">学生ID:</label>
                    <input type="text" id="search-id">
                </div>
                <div class="form-group">
                    <label for="search-name">姓名:</label>
                    <input type="text" id="search-name">
                </div>
                <div class="form-group">
                    <label for="search-phone">手机号:</label>
                    <input type="text" id="search-phone">
                </div>
                <div class="form-group">
                    <label for="search-idcared">身份证:</label>
                    <input type="text" id="search-idcared">
                </div>
                <div class="form-group">
                    <label for="search-clazzId">班级:</label>
                    <select id="search-clazzId" class="clazzId">
                        <option value="">请选择班级</option>
                    </select>
                </div>
            </div>
            <div class="btn-group">
                <button type="submit" class="btn-primary">查询</button>
                <button type="button" id="reset-search" class="btn-warning">重置</button>
            </div>
        </form>
    </div>

    <!-- 控制按钮 -->
    <div style="text-align: right; margin-bottom: 10px;">
        <button id="add-student-btn" class="btn-primary">添加学生</button>
        <button id="refresh-btn" class="btn-success">刷新列表</button>
    </div>

    <!-- 学生列表 -->
    <div>
        <table id="student-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>性别</th>
                <th>出生日期</th>
                <th>手机号</th>
                <th>身份证</th>
                <th>地址</th>
                <th>班级ID</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="student-list">
            <!-- 学生数据将通过JavaScript动态加载 -->
            </tbody>
        </table>
    </div>

    <!-- 分页 -->
    <div class="pagination" id="pagination">
        <!-- 分页按钮将通过JavaScript动态加载 -->
    </div>
</div>

<!-- 弹窗表单 -->
<div id="student-modal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2 id="modal-title">新增学生</h2>
            <span class="close">&times;</span>
        </div>
        <form id="student-form">
            <div class="form-group">
                <label for="id">学号:</label>
                <input type="text" id="id" required>
            </div>
            <div class="form-group">
                <label for="name">姓名:</label>
                <input type="text" id="name" required>
            </div>
            <div class="form-group">
                <label for="stuSex">性别:</label>
                <select id="stuSex" required>
                    <option value="">请选择性别</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div class="form-group">
                <label for="stuBirth">出生日期:</label>
                <input type="date" id="stuBirth" required>
            </div>
            <div class="form-group">
                <label for="stuPhone">手机号:</label>
                <input type="text" id="stuPhone" required>
            </div>
            <div class="form-group">
                <label for="stuIdcared">身份证:</label>
                <input type="text" id="stuIdcared" required>
            </div>
            <div class="form-group">
                <label for="stuAddr">地址:</label>
                <input type="text" id="stuAddr" required>
            </div>
            <div class="form-group">
                <label for="clazzId">班级:</label>
                <select id="clazzId" required class="clazzId">
                    <option value="">请选择班级</option>
                </select>
            </div>
            <div class="btn-group">
                <button type="submit" class="btn-primary">保存</button>
                <button type="button" id="modal-cancel-btn" class="btn-warning">取消</button>
            </div>
        </form>
    </div>
</div>

<script>
    let currentPage = 1;
    const pageSize = 5;
    let  openModalValue= false;//添加弹窗的标识符

    $(document).ready(function() {
        // 页面加载时获取学生列表和班级列表
        loadStudentList();
        loadClassList();

        // 绑定添加学生按钮事件
        $('#add-student-btn').on('click', function() {
            openModalValue=false;//添加弹窗的标识符
            openModal();
            resetForm();
        });

        // 绑定表单提交事件
        $('#student-form').on('submit', function(e) {
            e.preventDefault();
            saveStudent();
        });

        // 绑定取消按钮事件
        $('#modal-cancel-btn').on('click', function() {
            closeModal();
        });

        // 绑定关闭按钮事件
        $('.close').on('click', function() {
            closeModal();
        });

        // 点击弹窗外部关闭弹窗
        $('#student-modal').on('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });

        // 绑定刷新按钮事件
        $('#refresh-btn').on('click', function() {
            loadStudentList();
        });

        // 绑定查询表单提交事件
        $('#search-form').on('submit', function(e) {
            e.preventDefault();
            currentPage = 1;
            loadStudentList();
        });

        // 绑定重置查询按钮事件
        $('#reset-search').on('click', function() {
            $('#search-form')[0].reset();
            currentPage = 1;
            loadStudentList();
        });
    });

    // 打开弹窗
    function openModal() {
        $('#student-modal').css('display', 'block');
    }

    // 关闭弹窗
    function closeModal() {
        $('#student-modal').css('display', 'none');
    }

    // 加载学生列表
    function loadStudentList() {
        const params = {
            page: currentPage,
            pageSize: pageSize,
            id: $('#search-id').val(),
            name: $('#search-name').val(),
            phone: $('#search-phone').val(),
            idcared: $('#search-idcared').val(),
            clazzId: $('#search-clazzId').val()
        };

        $.ajax({
            url: '/student/listByPage',
            type: 'GET',
            data: params,
            success: function(response) {
                if (response.code === 200) {
                    renderStudentList(response.data.records);
                    renderPagination(response.data);
                } else {
                    showMessage('获取学生列表失败: ' + response.msg, 'error');
                }
            },
            error: function() {
                showMessage('获取学生列表失败，请稍后重试', 'error');
            }
        });
    }

    // 渲染学生列表
    function renderStudentList(students) {
        const tbody = $('#student-list');
        tbody.empty();

        if (students && students.length > 0) {
            students.forEach(function(student) {
                const row = `
                        <tr>
                            <td>${student.id || ''}</td>
                            <td>${student.name || ''}</td>
                            <td>${student.stuSex || ''}</td>
                            <td>${student.stuBirth || ''}</td>
                            <td>${student.stuPhone || ''}</td>
                            <td>${student.stuIdcared || ''}</td>
                            <td>${student.stuAddr || ''}</td>
                            <td>${student.clazzId || ''}</td>
                            <td class="action-buttons">
                                <button class="btn-primary" onclick="editStudent(${JSON.stringify(student).replace(/"/g, '&quot;')})">编辑</button>
                                <button class="btn-danger" onclick="deleteStudent('${student.id}')">删除</button>
                            </td>
                        </tr>
                    `;
                tbody.append(row);
            });
        } else {
            tbody.append('<tr><td colspan="9" style="text-align: center;">暂无学生数据</td></tr>');
        }
    }

    // 渲染分页
    function renderPagination(pageInfo) {
        const pagination = $('#pagination');
        pagination.empty();

        const totalPages = pageInfo.pages;

        // 上一页按钮
        if (currentPage > 1) {
            pagination.append(`<button onclick="changePage(${currentPage - 1})">上一页</button>`);
        }

        // 页码按钮
        for (let i = 1; i <= totalPages; i++) {
            if (i === currentPage) {
                pagination.append(`<button class="active">${i}</button>`);
            } else {
                pagination.append(`<button onclick="changePage(${i})">${i}</button>`);
            }
        }

        // 下一页按钮
        if (currentPage < totalPages) {
            pagination.append(`<button onclick="changePage(${currentPage + 1})">下一页</button>`);
        }
    }

    // 切换页面
    function changePage(page) {
        currentPage = page;
        loadStudentList();
    }

    // 加载班级列表（用于下拉框）
    function loadClassList() {
        $.ajax({
            url: '/clazz/list',
            type: 'GET',
            success: function(response) {
                if (response.code === 200) {
                    renderClassSelect(response.data);
                }
            }
        });
    }

    // 渲染班级下拉框
    function renderClassSelect(classes) {
        const classSelects = $('.clazzId');
        classSelects.empty();
        classSelects.append('<option value="">请选择班级</option>');

        if (classes && classes.length > 0) {
            classes.forEach(function(clazz) {
                $('.clazzId').append(`<option value="${clazz.clazzId}">${clazz.clazzName}</option>`);
            });
        }
    }

    // 保存学生（新增或更新）
    function saveStudent() {
        const id = $('#id').val();
        const name = $('#name').val();
        const stuSex = $('#stuSex').val();
        const stuBirth = $('#stuBirth').val();
        const stuPhone = $('#stuPhone').val();
        const stuIdcared = $('#stuIdcared').val();
        const stuAddr = $('#stuAddr').val();
        const clazzId = $('#clazzId').val();

        if (!id || !name || !stuSex || !stuBirth || !stuPhone || !stuIdcared || !stuAddr || !clazzId) {
            showMessage('请填写完整信息', 'error');
            return;
        }

        const data = {
            id:id,
            name: name,
            stuSex: stuSex,
            stuBirth: stuBirth,
            stuPhone: stuPhone,
            stuIdcared: stuIdcared,
            stuAddr: stuAddr,
            clazzId: clazzId
        };

        // 如果有id，则为更新操作
        const url = openModalValue ? '/student/update' : '/student/add';
        $.ajax({
            url: url,
            type: 'GET',
            data: data,
            success: function(response) {
                if (response.code === 200) {
                    showMessage(id ? '学生更新成功' : '学生新增成功', 'success');
                    closeModal();
                    loadStudentList();
                } else {
                    showMessage((id ? '学生更新失败: ' : '学生新增失败: ') + response.msg, 'error');
                }
            },
            error: function() {
                showMessage(id ? '学生更新失败，请稍后重试' : '学生新增失败，请稍后重试', 'error');
            }
        });
    }

    // 编辑学生
    function editStudent(student) {
        $('#id').val(student.id);
        $('#name').val(student.name);
        $('#stuSex').val(student.stuSex);
        $('#stuBirth').val(student.stuBirth);
        $('#stuPhone').val(student.stuPhone);
        $('#stuIdcared').val(student.stuIdcared);
        $('#stuAddr').val(student.stuAddr);
        $('#clazzId').val(student.clazzId);
        $('#modal-title').text('编辑学生');
        openModalValue=true;//修改页面
        openModal();
    }

    // 删除学生
    function deleteStudent(id) {
        if (!confirm('确定要删除这个学生吗？')) {
            return;
        }

        $.ajax({
            url: '/student/delete',
            type: 'GET',
            data: { id: id },
            success: function(response) {
                if (response.code === 200) {
                    showMessage('学生删除成功', 'success');
                    loadStudentList();
                } else {
                    showMessage('学生删除失败: ' + response.msg, 'error');
                }
            },
            error: function() {
                showMessage('学生删除失败，请稍后重试', 'error');
            }
        });
    }

    // 重置表单
    function resetForm() {
        $('#student-form')[0].reset();
        $('#id').val('');
        $('#modal-title').text('新增学生');
    }

    // 显示消息
    function showMessage(text, type) {
        const messageDiv = $('#message');
        messageDiv.removeClass('hidden success error').addClass(type).text(text);

        // 3秒后自动隐藏消息
        setTimeout(function() {
            messageDiv.addClass('hidden');
        }, 3000);
    }
</script>
</body>
</html>
